package cn.bdqn.service.sys.impl;

import cn.bdqn.dao.sys.SysEntryDao;
import cn.bdqn.entity.Entry;
import cn.bdqn.service.sys.SysEntryService;
import cn.bdqn.utils.UploadFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @ProjectName: ycbdqn
 * @Package: cn.bdqn.service.sys.impl
 * @Author: huat
 * @Date: 2020/3/12 20:22
 * @Version: 1.0
 */
@Service
public class SysEntryServiceImpl implements SysEntryService {
    @Autowired
    SysEntryDao sysEntryDao;

    @Value("${route_name}")
    private String routeName;//获取图片前面路径
    /**
     * 获取就业学生信息
     * @param page 起始页
     * @param limit 页面大小
     * @param studentName   就业学生姓名
     * @return
     */
    @Override
    public Map<String, Object> getEntry(int page, int limit, String studentName) {
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("count",sysEntryDao.countEntry(studentName));
        map.put("code",0);
        map.put("data",sysEntryDao.getAllEntry((page-1)*limit, limit, studentName));
        return map;
    }
    /**
     * 根据id删除就业学员
     * @param id 就业学生id
     * @return
     */
    @Override
    @Transactional
    public boolean deleteEntryById(int id) {
        return sysEntryDao.deleteEntryById(id)>0?true:false;
    }
    /**
     * 根据id查询就业学员
     * @param id
     * @return
     */
    @Override
    public Entry getEntryById(int id) {
        return sysEntryDao.getEntryById(id);
    }
    @Transactional
    @Override
    public String updateEntry(HttpServletRequest request, Entry entry) {
        MultipartHttpServletRequest mr = (MultipartHttpServletRequest) request;

        if(null!=mr.getMultiFileMap().get("entryImageUrl")){
            String fileName=mr.getFileMap().get("entryImageUrl").getOriginalFilename();
            String fileNameIsJPG=fileName.substring(fileName.lastIndexOf(".")+1);
            if(!"jpg".equalsIgnoreCase(fileNameIsJPG)){
                return "2";
            }
            String result= UploadFile.getFileURL(mr,"entryImageUrl");

            entry.setImageUrl(routeName+"/image/"+result);
        }
        return sysEntryDao.updateEntry(entry)>0?"1":"0";
    }
    /**
     * 新增就业学员
     * @param request
     * @param entry
     * @return
     */
    @Override
    public String addEntry(HttpServletRequest request, Entry entry) {
        if(entry.getEntryPhoneNo().length()!=11){
            return "3";
        }
        MultipartHttpServletRequest mr = (MultipartHttpServletRequest) request;

        if(null!=mr.getMultiFileMap().get("entryImageUrl")){
            String fileName=mr.getFileMap().get("entryImageUrl").getOriginalFilename();
            String fileNameIsJPG=fileName.substring(fileName.lastIndexOf(".")+1);
            if(!"jpg".equalsIgnoreCase(fileNameIsJPG)){
                return "2";
            }
            String result= UploadFile.getFileURL(mr,"entryImageUrl");

            entry.setImageUrl(routeName+"/image/"+result);
        }else{
            return "2";
        }

        return sysEntryDao.addEntry(entry)>0?"1":"0";
    }
}
